//
//  WinterViewController.m
//  ProjectA
//
//  Created by dlios on 15-3-5.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "WinterViewController.h"

@interface WinterViewController ()

@end

@implementation WinterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor greenColor];
    //创建UITableView
    self.tableV = [[UITableView alloc] initWithFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height - 49) style:(UITableViewStylePlain)];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    self.winterArray = [NSMutableArray array];
    //------------------解析数据-------------------------------//
    [[SeasonDataBase shareSpringDataBase] openDB];
    [[SeasonDataBase shareSpringDataBase] createTable];
    [AFNDownLoader getDataDownLoad:@"http://api.yi18.net/cook/list?id=85&limit=20&page=1&type=id" block:^(id myData) {
        [[SeasonDataBase shareSpringDataBase] deleteTable];
        [[SeasonDataBase shareSpringDataBase] createTable];
        NSMutableDictionary *dic = myData;
        NSMutableArray *myArray = [dic objectForKey:@"yi18"];
        for (NSDictionary *myDic in myArray) {
            SpringModel *winterModel = [[SpringModel alloc] init];
            winterModel.name = [myDic objectForKey:@"name"];
            winterModel.food = [myDic objectForKey:@"food"];
            winterModel.img = [myDic objectForKey:@"img"];
            winterModel.id = [myDic objectForKey:@"id"];
            [[SeasonDataBase shareSpringDataBase] insertSpringModel:winterModel];
            [self.winterArray addObject:winterModel];
            
        }
        [self.tableV reloadData];
        [self.HUD removeFromSuperview];
    }];
    //菊花
    self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    self.HUD.labelText = @" Loading";
    [self.view addSubview:self.HUD];
    [self.HUD showAnimated:YES whileExecutingBlock:^{
        
        [self showTime];
    } completionBlock:^{
        
        
    }];

    
    
    
    // Do any additional setup after loading the view.
}

- (void)showTime
{
    sleep(9);
}

//tableView必须实现的方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[SeasonDataBase shareSpringDataBase] selectSpring].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellMark = @"cellIndex";
    WinterTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellMark];
    if (cell == nil) {
        cell = [[WinterTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellMark];
    }
    
    SpringModel *data = [[[SeasonDataBase shareSpringDataBase]selectSpring] objectAtIndex:indexPath.row];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

    cell.nameLable.text = data.name;
    cell.foodLable.text = data.food;
    //万能公式拼接网络请求的图片
    NSString *urlStr = [NSString stringWithFormat:@"http://www.yi18.net/%@", data.img];
    NSURL *url = [NSURL URLWithString:urlStr];
    UIImage *placeholderImage = [UIImage imageNamed:@"homePage副本.png"];
    [cell.imageV sd_setImageWithURL:url placeholderImage:placeholderImage];
    
    return cell;
}
//cell行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //判断网络
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    if ([wifi currentReachabilityStatus] == NotReachable || [conn currentReachabilityStatus] == NotReachable) {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"网络不佳, 请稍后重试" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alterView show];
        [self.view addSubview:alterView];
    }else{
        
        

    WinterDetailViewController *detailVC = [[WinterDetailViewController alloc] init];
    SpringModel *data = [[[SeasonDataBase shareSpringDataBase] selectSpring] objectAtIndex:indexPath.row];
    detailVC.navigationItem.title = data.name;
    detailVC.id = data.id;
    [self.navigationController pushViewController:detailVC animated:YES];
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
